<template>
<!-- 收藏按钮 -->
  <van-button :icon="arr.is_collected ? 'star' : 'star-o'"

    :class="{
      collected:arr.is_collected  //根据收藏状态，应用.collected样式
    }"
  :loading="loading"
  @click='onCollect' />
</template>

<script>
import { addCollect, deleteCollect } from '@/api/article'
export default {
  name: 'CollectArticle',
  props: {
    arr: {

      type: Object,
      required: true
    }
  },
  data () {
    return {
      loading: false
    }
  },
  methods: {
    async onCollect () {
      this.loading = true
      try {
        if (this.arr.is_collected) {
          await deleteCollect(this.arr.art_id)
        } else {
          await addCollect(this.arr.art_id)
        }
        this.arr.is_collected = !this.arr.is_collected
      } catch (error) {
        // console.log(error)
        // console.log(this.arr)
        this.$toast.fail('操作失败，请重试')
      }

      this.loading = false
    }

  }
}
</script>

<style scoped lang='less'>
.collected {
  .van-icon {
    color: #ffa500;
  }
}
</style>
